#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/7/5 13:05
# @File    : 0demo.py
# @Description : -i http://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn


from tkinter import *
from tkinter import font
from tkinter import filedialog

# 支持的也太少了
from tkhtmlview import HTMLLabel,RenderHTML


root = Tk()
root.title("HTML文件显示")
root.geometry("600x600+300+100")

path = StringVar()
entry = Entry(font=("黑体", 20), textvariable = path)
entry.place(x=120, y=20)

def open_html_file():
    html_path = filedialog.askopenfilename(
        title='请选择本地HTML文件',
        filetypes=[('.html', '.html')],
    )

    if html_path and html_path.endswith(('.html')):
        path.set(html_path)
        with open(html_path, encoding="utf-8") as f:
            lb.set_html(f.read())

def showHtml():
    html = """
        <html>
        <body>
            <h1>一级标题</h1>
            <h2>二级标题</h2>
            <h3>出塞</h3>
            <h4>王昌龄</h4>
            <p>秦时明月汉时关，万里长征人未还。</p>
            <p>但使龙城飞将在，不教胡马度阴山。</p>
            <h4>友情链接</h4>
            <a href="https://www.zhihu.com/">知乎</a><br/>
            <a href="https://www.bilibili.com/">B站</a>
        </body>
        </html>
        """
    lb.set_html(html)


btn = Button(text="Show HTML", font=("黑体", 15), command=showHtml)
btn2 = Button(text="open", font=("黑体", 15), command=open_html_file)
btn.place(x=430, y=16)
btn2.place(x=10, y = 16)

html_content = StringVar()


lb = HTMLLabel(font=("黑体", 15), html="。。。")
lb.place(x=100, y=90)


root.mainloop()